﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Xml;

namespace omaura_oled
{
    public class UpdateSoftware
    {
        /// class to get the updates versions of the omaura software
        /// - access the xml file: omaura-oled-versions.xml on the omaura ftp
        /// - access the data of the xml to download the last version

        #region "Parameters
        //--
        public OmauraOled OledInterface;
        //--
        public string XML_Version_path = "http://www.omaura.com/downloads/omaura-oled/omaura-oled-versions.xml";
        public string OledSoftware_path = "http://www.omaura.com/downloads/omaura-oled/";

        //-- versions
        public string OledVersion_Beta;
        public string OledVersion_Stable;

        //-- urls
        public string OledVersion_Beta_url = "";
        public string OledVersion_Stable_url = "";
        //--
        #endregion "Parameters"

        #region "Constructors"
        //--
        public UpdateSoftware(OmauraOled parentOled)
        {
            OledInterface = parentOled;
        }
        //--
        #endregion "Constructors"

        #region "Methods"
        //--
        public void ReadVersion()
        {
            try
            {
                XmlTextReader reader = new XmlTextReader(XML_Version_path);

                string strURI = "";
                string strName = "";
                bool isBeta = false;
                bool isStable = false;

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        #region "element"
                        //--
                        case XmlNodeType.Element:
                            strURI = reader.NamespaceURI;
                            strName = reader.Name;

                            if (reader.Name.Equals("oled-soft"))
                            {
                                if (reader.HasAttributes)
                                {
                                    reader.MoveToFirstAttribute();
                                    if (reader.Name.Equals("type") && reader.Value.Equals("Beta"))
                                    {
                                        isBeta = true;
                                        isStable = false;
                                    }
                                    if (reader.Name.Equals("type") && reader.Value.Equals("Stable"))
                                    {
                                        isBeta = false;
                                        isStable = true;
                                    }
                                }
                            }

                            if (reader.Name.Equals("current"))
                            {
                                if(isBeta)
                                {
                                    reader.MoveToFirstAttribute();
                                    OledVersion_Beta = reader.Value;
                                    reader.MoveToElement();
                                    OledVersion_Beta_url = reader.ReadInnerXml();
                                }
                                if(isStable)
                                {
                                    reader.MoveToFirstAttribute();
                                    OledVersion_Stable = reader.Value;
                                    reader.MoveToElement();
                                    OledVersion_Stable_url = reader.ReadInnerXml();
                                }
                            }
                            break;
                        //--
                        #endregion "element"

                        default:
                            break;
                    }
                }
            }
            catch (XmlException e)
            {
                Log.Write("error occured: " + e.Message);
            }
        }
        public bool CompareVer1AndVer2(string ver1, string ver2)
        {
            //-- if ver1 > ver2 send true
            //-- else send false
            int[] v1 = new int[4];
            int[] v2 = new int[4];

            string[] version1 = ver1.Split('.');
            string[] version2 = ver2.Split('.');

            for (int i = 0; i < 4; i++)
            {
                v1[i] = Convert.ToInt32(version1[i]);
                v2[i] = Convert.ToInt32(version2[i]);
            }

            if (v1[0] > v2[0]
                || (v1[0] == v2[0] && v1[1] > v2[1])
                || (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] > v2[2])
                || (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] == v2[2] && v1[3] > v2[3])
                )
                return true;
            else
                return false;
        }
        //--
        #endregion "Methods"
    }
}
